/*
*************************************************************
 Copyright (C), 2016-2020 smartlinkMicro. Co., Ltd.  All rights reserved.
 Filename:
 Complier:    ARM-MDK 5.xx
 Target OS:   none
 Target HW:
 Author:        htyi
 Version :      V1.0.0
 Date:
 Description:
 History:
     <author>     <time>     <version >   <desc>

*************************************************************
*/
#include "Includes.h"
#include "emv_if.h"
#include "sl2x23.h"
#include "Rfid_interface.h"
#include "lpcd.h"
#include "SVS_Station.h"

extern uint8_t  card_A;
extern uint8_t  card_B;
extern uint8_t  card_F;
extern uint8_t  card_V;
extern uint8_t  sak;
extern uint16_t atqa;

int8_t nfc_demo(void) 
{
    volatile uint8_t card_det = 0, g_ret, demo_step = 0;
    
    while(1) {
        switch(demo_step) {
            case 0://询卡
                card_A = 0;
                card_B = 0;
                card_F = 0;
                card_V = 0;
                g_ask_block_flag = 0;
                #ifdef SPD_MODE
                pcd_sleep_exit(); //Soft power on
                #endif
                pcd_set_rate(1);
                g_ret = pcd_polling(g_tag_info.serial_num);
                if(g_ret == 0) demo_step = 1;
                break;
            case 1:
                if(card_A) {
                    if((sak == 0x08) || (sak == 0x18)) { //MIFARE
                        NFC_A_Mifare_Test(g_tag_info.serial_num);
                        demo_step = 0;
                        card_det  = 1;
                        break;
                    }
                    if(atqa == 0x000c) {
                        NFC_A_Tag1_Test(g_tag_info.serial_num);
                        demo_step = 0;
                        card_det  = 1;
                        break;
                    }
                    if(atqa == 0x4400) {
                        NFC_A_Tag2_Test(g_tag_info.serial_num);
                        demo_step = 0;
                        card_det  = 1;
                        break;
                    }
                }
                g_ret = pcd_active(1);
                if(g_ret == 0) {
                    demo_step = 2;
                } else {
                    demo_step = 0;
                }
                break;
            case 2:
                if(card_A) {
                    Iblock_getrand(1);
                }
                if(card_B) {
                    g_ask_block_flag = 1;
                    g_ret = get_idcard_num(g_tag_info.serial_num);
                    if(g_ret == 0) {
                        g_tag_info.uid_length = 10;
                        PRINT_BUF("ID_NUM: ", g_tag_info.serial_num, g_tag_info.uid_length);
                        printf("## test end##\r\n\r\n");
                    } else {
                        Iblock_getrand(1);
                    }
                }
                if(card_F) {
                    NFC_F_Tag3_Test(g_tag_info.serial_num);
                }
                if(card_V) {
                    NFC_V_Tag5_Test(g_tag_info.serial_num);
                }
                demo_step = 0;
                card_det  = 1;
                break;
            default:
                demo_step = 0;
                card_det  = 0;
                break;
        }
        if(demo_step == 0) {
            pcd_antenna_off();
            delay_1ms(1);
            #ifdef SPD_MODE
            pcd_sleep();      //Soft power off
            #endif
            break;
        }
    }
    return card_det;
}

void lpcd_demo(void) {
    uint8_t adc_value;
    uint32_t wait_cnt = 0;
    irq_flag_io = 0;
    lpcd_cfg.t1 = LPCD_DETECT_PERIOD;      //0x12 300ms; //0x20 500ms ; //0x38 800ms
    lpcd_cfg.sense = LPCD_DETECT_SENS;
    //printf("******* start lpcd_demo() *******\r\n");
    osc_calib();
    lpcd_init(1, 0x40); //fst_en
    lpcd_entry();
    while(1) {
    #ifdef NOT_IRQ
        if(!(INT_PIN == 0))
    #else
        if(irq_flag_io)
    #endif
        {
            //printf("...................................\r\n");
            //printf("\r\n******* l、pcd_exit() *********\r\n");
            irq_flag_io = 0;
            adc_value = lpcd_exit();
            Rfid_Init();
            //printf("******* 2、nfc_demo() *********\r\n");
            lpcd_init(nfc_demo(), adc_value);
            pcd_antenna_off();
            //printf("******* 3、lpcd_entry() *******\r\n");
            lpcd_entry();
        } else {
            wait_cnt++;
            if(wait_cnt == 500000) {
                wait_cnt = 0;
                printf(".");
            }
        }
    }
}


void sl2823_test(void) {
    printf("\r\nsl2x23_complete_example_V1.0\r\n");
    delay_1ms(2);
    spi_wr_test();
    Rfid_Init();
    pcd_default_info();
    irq_flag_io = 0;
    #ifdef LPCD_MODE
    lpcd_demo();
    #else
    while(1) {
        nfc_demo();
        delay_1ms(500);
        printf(".");
    }
    #endif
}

#ifndef USE_STDPERIPH_DRIVER
int main() {
    sl2823_test();
}
#endif


extern void nfc_irq_exit_config(void);
extern void SPI1_Init(void);

void nfc_init(void)
{
    SPI1_Init();
    nfc_irq_exit_config();
    
    spi_wr_test();
    
	Rfid_Init();
	pcd_default_info();
	irq_flag_io = 0;

	lpcd_cfg.t1    = LPCD_DETECT_PERIOD; //0x12 300ms; //0x20 500ms ; //0x38 800ms
	lpcd_cfg.sense = LPCD_DETECT_SENS;
	printf("******* start lpcd_demo() *******\r\n");
	osc_calib();
	lpcd_init(1, 0x40);
	lpcd_entry();
}

uint8_t nfc_process(void)
{
    uint8_t card_flag = 0;
    uint8_t adc_value = 0;

    if(irq_flag_io)
    {
        //printf("...................................\r\n");
        //printf("\r\n******* l.pcd_exit() *********\r\n");
        irq_flag_io = 0;
        adc_value = lpcd_exit();
        Rfid_Init();
        //printf("******* 2.nfc_demo() *********\r\n");        
        card_flag = nfc_demo();
        if(card_flag)
        {
            //strcpy((char *)globalVal.serial_num, (char *)g_tag_info.serial_num);
        }
        lpcd_init(card_flag, adc_value);
        pcd_antenna_off();
        //printf("******* 3.lpcd_entry() *******\r\n");
        lpcd_entry();

        //wangtao
        if(!GunIsPlugin())
        {
            printf("Please plugin gun first!!!\r\n");
            return 0;
        }
        MakeCardSwipAction();
    }
    
    return card_flag;
}

#include "stm32f10x.h"


//PA15
void nfc_irq_exit_config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure; 
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    /*开启按键GPIO口的时钟*/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel                   = EXTI15_10_IRQn;   /* 配置中断源：NFC-IRQ */
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 15;               /* 配置抢占优先级      */
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;                /* 配置子优先级        */
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;           /* 使能中断通道        */
    NVIC_Init(&NVIC_InitStructure);

    /*--------------------------配置引脚-----------------------------*/	
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* -------------选择EXTI的信号源 -------------*/
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,  GPIO_PinSource15);
    EXTI_InitStructure.EXTI_Line    = EXTI_Line15;
    EXTI_InitStructure.EXTI_Mode    = EXTI_Mode_Interrupt;          /* EXTI为中断模式 */
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;         /* 下降沿中断     */
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;                       /* 使能中断       */
    EXTI_Init(&EXTI_InitStructure); 
}


//NFC-IRQ->PA15
void EXTI15_10_IRQHandler(void)
{
    if(EXTI_GetITStatus(EXTI_Line15)!=RESET) //判断某个线上的中断是否发生 
    {
        irq_flag_io=1;
        EXTI_ClearITPendingBit(EXTI_Line15); //清除 LINE 上的中断标志位 
    }    
}

